package com.rtx.edi.platform.dash.error.service.impl;



import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.edi.model.base.Constant;
import com.rtx.edi.model.error.po.ApiReceiptFinish;
import com.rtx.edi.platform.dash.error.mapper.PlApiReceiptFinishMapper;
import com.rtx.edi.platform.dash.error.service.PlApiReceiptFinishService;
import com.rtx.wms.std.common.bean.WmsConstant;

@Service
public class PlApiReceiptFinishServiceImpl extends ServiceImpl<PlApiReceiptFinishMapper,ApiReceiptFinish> implements PlApiReceiptFinishService {
	
	@DataSourceAnnotation
	@Override
	public void updApiReceiptFinish(String whseid, ApiReceiptFinish apiReceiptFinish) {
		
		LambdaUpdateWrapper<ApiReceiptFinish> update=Wrappers.lambdaUpdate();
		update.eq( ApiReceiptFinish::getSerialkey, apiReceiptFinish.getSerialkey())
		.set(ApiReceiptFinish::getEdiflag, Constant.DEFAULT_FLAG)
        .set(ApiReceiptFinish::getRtxqueue,"0");
		baseMapper.update(null, update);
	}

	@DataSourceAnnotation
	@Override
	public void selectApiReceiptFinishErrorForList(String datasourcekey,Page<ApiReceiptFinish> pageParam, ApiReceiptFinish error) {
		LambdaQueryWrapper<ApiReceiptFinish> query =Wrappers.lambdaQuery();
		query.eq(ApiReceiptFinish::getEdiflag,WmsConstant.FAIL_FLAG).or().gt(ApiReceiptFinish::getRtxqueue, WmsConstant.DEFAULT_COUNT);
		
		if(error!=null) {
			query.eq( error.getExternreceiptkey()!=null,ApiReceiptFinish::getExternreceiptkey, error.getExternreceiptkey())
			.eq( error.getSerialkey()!=null,ApiReceiptFinish::getSerialkey, error.getSerialkey())
			.eq( error.getWhseid()!=null,ApiReceiptFinish::getWhseid, error.getWhseid()) ;
		}
		baseMapper.selectPage(pageParam,query);
	}

	@DataSourceAnnotation
	@Override
	public void updateSuccess(String whseid, ApiReceiptFinish apiReceiptFinish) {
		LambdaUpdateWrapper<ApiReceiptFinish> update =Wrappers.lambdaUpdate();
		 update.eq(ApiReceiptFinish::getSerialkey,apiReceiptFinish.getSerialkey())
		 .set(ApiReceiptFinish::getEdiflag,Constant.SUCCESS_FLAG)
		 .set(ApiReceiptFinish::getNotes,new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date())+"-手动标记为处理成功")
		 .set(ApiReceiptFinish::getRtxqueue,0);
		 baseMapper.update(null, update) ;
	}
}
